package com.zrrd.blog.comfig;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.io.IOException;

//开启当前微服务对token的验证
@Configuration
public class JwtTokenStoreConfig {
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter converter=new JwtAccessTokenConverter();
        //采用非对称加密，资源服务器使用公钥解密,公钥就是public.txt
        ClassPathResource resource=new ClassPathResource("public.txt");
        String publickey=null;
        try{
            publickey = IOUtils.toString(resource.getInputStream(),"UTF-8");
        }catch (IOException e){
            e.printStackTrace();
        }
        converter.setVerifierKey(publickey);
        return converter;
    }

    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
}
